07. Client Side & Server Side Example

Client Side & Server Side Example Header

Client Side & Server Side Example

ND#0001 C3 L3 A04 Client Side & Server Side Walkthrough

Client Side & Server Side Example Summary

Let's focus in on the actual POST request, which is an object passed as the second parameter to fetch(). The First parameter is the URL we want to make the POST request to.

 {
      method: 'POST', 
      credentials: 'same-origin',
      headers: {
          'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
 }

The credentials and headers are pretty boilerplate, but necessary for a successful POST request. The most important thing to notice is that Content-Type is set to json because we will be handling our data with JSON, for the most part.

Now we get to the juicy parts: the method is set to POST because we are accessing the POST route we setup in server.js. If we wanted to make a GET request from the client side, the method would be GET. The body of the request is the part we are most interested in because this is how we will access the data on the server side. When sending data to a web server, the data has to be a string. We can convert a JavaScript object into a string using the JavaScript method JSON.stringify(), which turns JavaScript objects and JSON data into a string for our server to receive the information. In this example, we are turning the JavaScript object passed in the data parameter into a string.

Client Side & Server Side Quiz

Check all that are true about server side and client side programming:

SOLUTION:
  • Server code and client side code belong in separate files at different levels of your project hierarchy on the browser
  • A route created on the server side can be accessed through a GET or POST request made on the client side
  • Server code manages routing of data separate from the browser